#ifndef WGLRENDER_GUARD
#define WGLRENDER_GUARD

#include <windows.h>
#include <iostream>
#include "Render.h"
#include "Win32Window.h"

#include <gl\gl.h>
#include <gl\glu.h>


class WGLRender: public Render
{
public:
	WGLRender(HWND hwnd):hWnd(hwnd),hDC(GetDC(hWnd)){}
	void init(int bits)
	{
		static	PIXELFORMATDESCRIPTOR pfd=				// pfd Tells Windows How We Want Things To Be
		{
			sizeof(PIXELFORMATDESCRIPTOR),				// Size Of This Pixel Format Descriptor
			1,											// Version Number
			PFD_DRAW_TO_WINDOW |						// Format Must Support Window
			PFD_SUPPORT_OPENGL |						// Format Must Support OpenGL
			PFD_DOUBLEBUFFER,							// Must Support Double Buffering
			PFD_TYPE_RGBA,								// Request An RGBA Format
			bits,										// Select Our Color Depth
			0, 0, 0, 0, 0, 0,							// Color Bits Ignored
			0,											// No Alpha Buffer
			0,											// Shift Bit Ignored
			0,											// No Accumulation Buffer
			0, 0, 0, 0,									// Accumulation Bits Ignored
			16,											// 16Bit Z-Buffer (Depth Buffer)  
			0,											// No Stencil Buffer
			0,											// No Auxiliary Buffer
			PFD_MAIN_PLANE,								// Main Drawing Layer
			0,											// Reserved
			0, 0, 0										// Layer Masks Ignored
		};
		hDC=GetDC(hWnd);
		GLuint PixelFormat=ChoosePixelFormat(hDC,&pfd);
		SetPixelFormat(hDC,PixelFormat,&pfd);
		hRC=wglCreateContext(hDC);	

	}
	void begin()
	{
		registThread();
		std::cout<<"WGLRender::begin()"<<std::endl;
	}
	void end()
	{
		ungistThread();
		std::cout<<"WGLRender::end()"<<std::endl;
	}
	void draw()
	{
		std::cout<<"WGLRender::draw()"<<std::endl;
		/*glViewport(0,0,640,480);						// Reset The Current Viewport

		glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
		glLoadIdentity();									// Reset The Projection Matrix

		// Calculate The Aspect Ratio Of The Window
		gluPerspective(45.0f,(GLfloat)640/(GLfloat)480,0.1f,100.0f);

		glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
		glLoadIdentity();

		glShadeModel(GL_SMOOTH);							// Enable Smooth Shading
		glClearColor(1.0f, 0.0f, 0.0f, 0.5f);				// Black Background
		glClearDepth(1.0f);									// Depth Buffer Setup
		glEnable(GL_DEPTH_TEST);							// Enables Depth Testing
		glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do
		glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	


		//glColor3f(1.0,1.0,1.0);
		//glBegin(GL_POLYGON);*/
		//clear();

		glColor3f(0,1,0);

		glBegin(GL_POLYGON);
		glVertex2f(0,0);
		glVertex2f(0,3);
		glVertex2f(4,3);
		glVertex2f(6,1.5);
		glVertex2f(4,0);
		glEnd();
		//glFlush();
		//swap();
	}

	void setClearColor(float r, float g, float b, float a)
	{
		glClearColor(r,g,b,a);
	}

	void clear()
	{	
		glClear(GL_COLOR_BUFFER_BIT);
	}

	void setColor(float r,float g,float b,float a)
	{
		glColor4f(r,g,b,a);
	}
	void swap()
	{
		SwapBuffers(hDC);
	}
	void registThread()//private
	{
		wglMakeCurrent(hDC,hRC);
	}
	void ungistThread()//private
	{
		wglMakeCurrent(NULL,NULL);
	}

	~WGLRender()
	{
		wglMakeCurrent(NULL,NULL);					// Are We Able To Release The DC And RC Contexts?
		
		if (hRC!=NULL)wglDeleteContext(hRC);					// Are We Able To Delete The RC?
		hRC=NULL;										// Set RC To NULL
		
		if (hWnd!=NULL&&hDC!=NULL)ReleaseDC(hWnd,hDC);					// Are We Able To Release The DC
		hDC=NULL;

		if (hWnd!=NULL)DestroyWindow(hWnd);					// Are We Able To Destroy The Window?
		hWnd=NULL;										// Set hWnd To NULL
								// Set hInstance To NULL
	}

	int renderIndexedVertexArray(float* vertices, int vertices_count,int* indices,int indices_count)
	{
		glEnableClientState(GL_VERTEX_ARRAY);
		glVertexPointer(3,GL_FLOAT,0,vertices);
		glDrawElements(GL_TRIANGLES,3,GL_UNSIGNED_INT,indices);

		return 0;
	}
private:
	HDC			hDC;		// Private GDI Device Context
	HGLRC		hRC;		// Permanent Rendering Context
	HWND		hWnd;		// Holds Our Window Handle

};

#endif
